#include "audioview.h"

audioview::audioview(AudioPlayerBackend * music, QGraphicsScene * scene, QSettings * theme): QGraphicsItemGroup(NULL, scene)
{
        this->x = 0;
        this->y =140;
        this->music = music;
        this->themeSettings = theme;
        init();
}

void audioview::init()
{
        this->setHandlesChildEvents(false);

        /*
        * Search all .so files
        */
        QDir plugdir = QDir::current();
        QStringList filters;
        filters.append(QString("*.so"));
        QStringList pluglist = plugdir.entryList(filters, QDir::Files);

        /*
        * Load and cast plugins to ensure they are of WidgetInterface type
        * Do NOT delete the QPluginLoader
        */
        WidgetInterface * widget = 0;
        QObject * plug = 0;
        QPluginLoader * loader = 0;
        for(int i=0;i<pluglist.size();i++){

            loader = new QPluginLoader(QDir::currentPath() + "/" + pluglist.at(i));   //create a pluginloader
            if(loader == 0){
                qDebug() << "QPluginLoader failed: " << QDir::currentPath() + "/" + pluglist.at(i);
            }else{
                qDebug() << "QPluginLoader created: " << QDir::currentPath() + "/" + pluglist.at(i);
            }
            plug = loader->instance();   //instance the plugin

            qDebug() << "parsing " << pluglist.at(i);

            /*
            * verify the plugin type
            * do NOT delete the instanciate object
            */
            widget = qobject_cast<WidgetInterface *>(plug);
            if(widget != 0){
                qDebug() << "WidgetInterface plugins found: " << pluglist.at(i);
                widgetLoaders.insert(widget->identifier(),loader);
                //delete widget;
                //widget = 0;
            }else{
                if(plug != 0){
                    qDebug() << pluglist.at(i) << " instanciation ok, but not of type WidgetInterface";
                    //delete plug;
                    //plug = 0;
                }else{
                    qDebug() << pluglist.at(i) << " didn't instanciate";
                }
                if(loader != 0){
                    //delete loader;
                    //loader = 0;
                }
            }

        }

        QString name = themeSettings->value("name").toString();
        QStringList widgets = themeSettings->childGroups();

        foreach(QString wid, widgets){
            QString type = themeSettings->value(wid + "/type").toString();

            double posx,posy;

            posx = themeSettings->value(wid + "/posx").toDouble();
            posy = themeSettings->value(wid + "/posy").toDouble();

            if(type == "browser"){

                QString icon = "/" + name + themeSettings->value(wid + "/icon").toString();
                collectionSwitcherWid * switcher = new collectionSwitcherWid(icon);
                this->addToGroup(switcher);
                switcher->setPos(posx,posy);

                connect(switcher, SIGNAL(collectionSwitcherPressed()), this, SLOT(showBrowser()));

            }else if(type == "forwrew"){

                QString icon = "/" + name + themeSettings->value(wid + "/icon").toString();
                forwrewWid * forwrew = new forwrewWid(icon);
                this->addToGroup(forwrew);
                forwrew->setPos(posx,posy);

                this->connect(forwrew, SIGNAL(forwardStarted()), this, SLOT(forwardButton()));
                this->connect(forwrew, SIGNAL(rewStarted()), this, SLOT(rewButton()));
                this->connect(forwrew, SIGNAL(forwardEnded(int)), this, SLOT(forwardButton(int)));
                this->connect(forwrew, SIGNAL(rewEnded(int)), this, SLOT(rewButton(int)));

            }else if(type == "next"){

                QString icon = "/" + name + themeSettings->value(wid + "/icon").toString();
                nextWid * next = new nextWid(icon);
                this->addToGroup(next);
                next->setPos(posx,posy);

                this->connect(next, SIGNAL(nextPressed()), music, SLOT(next()));

            }else if(type == "playpause"){

                QString playIcon = "/" + name + themeSettings->value(wid + "/playIcon").toString();
                QString pauseIcon = "/" + name + themeSettings->value(wid + "/pauseIcon").toString();

                playWid * play = new playWid(music->isPlaying(), playIcon, pauseIcon);
                this->addToGroup(play);
                play->setPos(posx,posy);

                this->connect(play, SIGNAL(pauseToggled()), music, SLOT(togglePause()));
                this->connect(music, SIGNAL(stateChanged(bool)), play, SLOT(updateStatus(bool)));

            }else if(type == "prev"){

                QString icon = "/" + name + themeSettings->value(wid + "/icon").toString();
                prevWid * prev = new prevWid(icon);
                this->addToGroup(prev);
                prev->setPos(posx,posy);

                this->connect(prev, SIGNAL(prevPressed()), music, SLOT(prev()));

            }else if(type == "random"){

                QString onIcon = "/" + name + themeSettings->value(wid + "/onIcon").toString();
                QString offIcon = "/" + name + themeSettings->value(wid + "/offIcon").toString();

                randomWid * random = new randomWid(onIcon, offIcon, music->getRandom());
                this->addToGroup(random);
                random->setPos(posx,posy);

                this->connect(random, SIGNAL(randomToggled()), music, SLOT(toggleRandom()));
                this->connect(music, SIGNAL(randomStatusChanged(bool)), random, SLOT(updateStatus(bool)));

            }else if(type == "repeat"){

                QString onIcon = "/" + name + themeSettings->value(wid + "/onIcon").toString();
                QString offIcon = "/" + name + themeSettings->value(wid + "/offIcon").toString();

                repeatWid * repeat = new repeatWid(onIcon, offIcon, music->getRepeat());
                this->addToGroup(repeat);
                repeat->setPos(posx,posy);

                this->connect(repeat, SIGNAL(repeatToggled()), music, SLOT(toggleRepeat()));
                this->connect(music, SIGNAL(repeatStatusChanged(bool)), repeat, SLOT(updateStatus(bool)));

            }else if(type == "title"){

                int fsize = themeSettings->value(wid + "/fsize").toInt();
                QColor color;
                color.setRed(themeSettings->value(wid + "/r").toInt());
                color.setGreen(themeSettings->value(wid + "/g").toInt());
                color.setBlue(themeSettings->value(wid + "/b").toInt());

                titleText * title = new titleText(music,color, fsize);
                this->addToGroup(title);
                title->setPos(this->scene()->width() / 2, posy);

                this->connect(music, SIGNAL(songChanged()), title, SLOT(updateCurrentTitle()));

                title->updateCurrentTitle();

            }else if(type == "volume"){

                QString icon = "/" + name + themeSettings->value(wid + "/icon").toString();
                volumeWidget * volume = new volumeWidget(icon);
                this->addToGroup(volume);
                volume->setPos(posx,posy);

                this->connect(volume, SIGNAL(volumeChanged(int)), music, SLOT(setVolume(int)));

            }
        }

        //animation = new QPropertyAnimation(random, "pos");
        //animation->setDuration(3000);

        //animation->setKeyValueAt(0, QPointF(800, 0));
        //animation->setKeyValueAt(0.5, QPointF(400,0));
        //animation->setKeyValueAt(1, QPointF((this->scene()->width() / 4 - random->boundingRect().width()) - 50, this->scene()->height() - random->boundingRect().height()));

        //animation->setStartValue(QPointF(800, 0));
        //animation->setEndValue(QPointF((this->scene()->width() / 4 - random->boundingRect().width()) - 50, this->scene()->height() - random->boundingRect().height()));
        //animation->setEasingCurve(QEasingCurve::OutBounce);
        //animation->start();

}

void audioview::showBrowser()
{
    qDebug() << "showBrowser()";
    emit showBrowserButtonClicked();
}

void audioview::rewButton(int ms)
{
        if(ms == 0){
                music->pause();
        }else{
                int value = music->getElapsedTime() - ms * 8 / 1000;
                music->seek(value);
                music->play();
        }
}

void audioview::forwardButton(int ms)
{
        if(ms == 0){
                music->pause();
        }else{
                int value = music->getElapsedTime() + ms * 5 / 1000;
                music->seek(value);
                music->play();
        }
}
